$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fyvideo/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '内容', name: 'content', index: 'content', width: 100 },
			{ label: '视频/图片', name: 'videoUrl', index: 'video_url', width: 200},
			{ label: '视频封面', name: 'coverUrl', index: 'cover_url', width: 80,formatter: function(cellvalue, options, rowdata) {
					if(rowdata.isType == 2&&rowdata.coverUrl!=null){
                        return '<img src='+rowdata.coverUrl+' width="50" height="50"/>';
					}else{
						return '';
					}


                } },
			{ label: '发布时间戳', name: 'creattime', index: 'creattime', width: 70 },
			{ label: '发布人', name: 'userId', index: 'user_id', width: 70,formatter: function(cellvalue, options, rowdata) {
                    var username ='' ;
                    if(rowdata.userId!=null && rowdata.userId!=''){
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/fyusers/info/"+rowdata.userId,
                            success: function(r){
                                username = r.fyUsers.userName;

                            }
                        });
					}

                    return username;
                } },
			{ label: '点赞数', name: 'greatNumber', index: 'great_number', width: 50 },
			{ label: '评论数', name: 'commentNumble', index: 'comment_numble', width: 50 },
			{ label: '类型', name: 'isType', index: 'is_type', width: 60,formatter: function(item, index) {
                    if (item === 1) {
                        return '<span class="label label-warning">图片</span>';
                    }
                    if (item === 2) {
                        return '<span class="label label-success">视频</span>';
                    }
                } },
			{ label: '发布时间', name: 'creatstrtime', index: 'creatstrtime', width: 70 },
			{ label: '观看次数', name: 'watchNumber', index: 'watch_number', width: 50 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

});

var vm = new Vue({
	el:'#rrapp',
	data:{
        fileList1: [],
		showList: true,
		title: null,
		fyVideo: {}

    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fyVideo = {};
            vm.fileList1 = [];
            vm.fyVideo.isType = 2;
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},

        handle:function (){
            console.log("取到的值是"+vm.fyVideo.isType);
        },


		saveOrUpdate: function (event) {
			var url = vm.fyVideo.id == null ? "sys/fyvideo/save" : "sys/fyvideo/update";
            vm.fyVideo.videoUrl = '';
            vm.fyVideo.coverUrl = '';
            if(vm.fileList1.length>0){
                vm.fyVideo.videoUrl = vm.fileList1[0].url;
                if(vm.fileList1[0].fmurl !=null){
                    vm.fyVideo.coverUrl = vm.fileList1[0].fmurl;
                }
            }


			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.fyVideo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fyvideo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);

						}
					}
				});
			});
		},
        handleRemove1: function(file, fileList) {
            console.log(fileList);
            if(fileList.length>0){
                vm.fileList1 = [];
                for(var i=0;i<fileList.length;i++){
                	if(fileList[i].fmurl!=null){
                        vm.fileList1.push({name: fileList[i].name,url: fileList[i].url,fmurl:fileList[i].fmurl});
					}else{
                        vm.fileList1.push({name: fileList[i].name,url: fileList[i].url});
					}

                }
            }
            console.log(vm.fileList1);

        },
        handlePreview: function(file) {
            console.log(file);
        },

        handsuccess1: function(response, file, fileList) {
			if(response.fmurl!=null){
                vm.fileList1.push({name: response.url,url: response.url,fmurl:response.fmurl});
			}else{
                vm.fileList1.push({name: response.url,url: response.url});
			}

        },
		getInfo: function(id){
			$.get(baseURL + "sys/fyvideo/info/"+id, function(r){
                vm.fyVideo = r.fyVideo;

                vm.fileList1 = [];
                vm.fileList1.push({name: r.fyVideo.videoUrl,url: r.fyVideo.videoUrl,fmurl:r.fyVideo.coverUrl});
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});